import {getProductData} from "@/api/data/ProductData";
import axiosInstance from "./axios"

/*const request = axios.create({
    baseURL: "",
    timeout: 5000,
});*/
const request = axiosInstance;
// 获取所有产品
export const getAllApi = () => {

    return request.get("/product/list",{params:{page:1,size:20}});
}

//根据分类获取商品
export const getApByTypeApi = (type,page,size) => {
    console.log(type,page,size);
    return request.get('/product/listByCategory',{params:{categoryId:type,page:page,size:size}})
}

//根据目标人群获取商品
export const getApByTargetApi = (target,page,size) => {
    return request.get('/product/listByAudience',{params:{targetAudienceId:target,page:page,size:size}})
}

// 根据价格区间获取商品
export const getApByPriceApi = (min, max) => {
    return getProductData().then(res => {
        return res.filter(item => item.price >= min && item.price <= max);
    })
}

// 根据关键字搜索商品
export const searchApi = (keyword,page,size) => {
    return request.get('/search', { params: { keyword:keyword ,page:page,size:size} })
}
export const getProductByIdApi = (id) => {
    console.log("商品请求"+id)
    return request.get('/product/getById/'+id)
}

export const getUserData = () => {
    localStorage.getItem('user');
}

export const adminGetProductApi = (page, size) => {
    console.log("adminGetProductApi"+localStorage.getItem("token"))
    return request.get('/admin/products',{params:{page:page,pageSize:size}})
}
// 添加更新商品状态的方法
export const updateStatusApi = (id, newStatus) => {
    let status
    newStatus ? status = 1 : status = 0
    return request.put('/admin/product/status/'+id+'/'+status)
}

// 更新商品信息
export const updateProductApi = (product) => {
    return request.put('/admin/updateProduct', product)
}

// 添加新商品
export const addProductApi = (product) => {
    return request.post('/admin/addProduct', product)
}
export const uploadImageApi = (file) => {
    return request.post('/upload', file, {
        headers: {
            'Content-Type': 'multipart/form-data' // 设置 Content-Type 为 multipart/form-data
        }
    });
}
export const getStatisticsApi = () => {
    return request.get('/statistics/category')
}
export const removeFavoriteApi=(id)=>{
    return request.delete('/user/favorite/'+id)
}
export const addFavoriteApi=(id)=>{
    return request.post('/user/favorite/'+id)
}
export const getByPriceRange=(page,size,minPrice,maxPrice)=>{
    return request.get('/product/priceRange',{params:{page:page,size:size,minPrice:minPrice,maxPrice:maxPrice}})
}